package com.mohe.shanpao.netty.protobuf;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;

import com.mohe.shanpao.netty.protobuf.CommandProto.Command;

public class HttpClientDemo {

	public static void main(String[] args) throws ClientProtocolException, IOException {
		CloseableHttpClient client = HttpClientBuilder.create().build();
		
		HttpPost httppost = new HttpPost("http://127.0.0.1:9900");
		httppost.addHeader("Connection","close");
//		httppost.addHeader("Connection","keep-alive");
		
		CommandProto.Command.Builder c = CommandProto.Command.newBuilder();
		c.setCmd("send");
		c.setName("proto");
		c.setKeycode(20);
		c.setPosX(1);
		c.setPosY(2);
		Command cc = c.build();
		ByteArrayEntity entity = new ByteArrayEntity(cc.toByteArray());
		httppost.setEntity(entity);
		
//		StringEntity e = new StringEntity("aaaaaaaaaaa");
//		httppost.setEntity(e);
		CloseableHttpResponse response = client.execute(httppost);
		HttpEntity receiveEntity = response.getEntity();
		
		System.out.println(response.getStatusLine());
		
		if (receiveEntity != null) {
			System.out.println("Response content length: " + receiveEntity.getContentLength());
		}
	}

}
